package com.kedacom.demo.websocket.client;

import java.util.List;
import java.util.Map;

import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.HandshakeResponse;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.PongMessage;
import javax.websocket.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ClientEndpoint(configurator = MyWebSocketClient.ClientConfigurator.class)
public class MyWebSocketClient {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@OnOpen
	public void onOpen(Session session) {
		logger.info("已连接，session id: {}", session.getId());
	}

	@OnMessage
	public void onMessage(String message) {
		logger.info("收到消息：{}", message);
	}

	@OnMessage
	public void onPong(PongMessage pongMessage) {
		logger.info("收到PongMessage消息：{}", pongMessage);
	}

	@OnClose
	public void onClose(Session session) {
		logger.info("连接关闭，session id: {}", session.getId());
	}

	@OnError
	public void onError(Throwable t) {
		logger.error("客户端出错：{}", t.getMessage());
	}

	public static class ClientConfigurator extends ClientEndpointConfig.Configurator {

		@Override
		public void beforeRequest(Map<String, List<String>> headers) {
			super.beforeRequest(headers);
		}

		@Override
		public void afterResponse(HandshakeResponse hr) {
			super.afterResponse(hr);
		}
	}

}
